package org.chunchu.todo.mvp.impl;

import javax.inject.Inject;

import org.chunchu.todo.Main;
import org.chunchu.todo.dao.CategoryDao;
import org.chunchu.todo.model.Category;
import org.mvp.AbstractPresenter;

import roboguice.inject.InjectExtra;

public class AddCategoryPresenter extends AbstractPresenter<AddCategory> {

	@Inject
	CategoryDao categoryDao;

	@InjectExtra(AddCategory.EXTRA_CATEGORY)
	Category extraCategory;

	@Override
	public void initializeWith(AddCategory activity) {
		super.initializeWith(activity);
		if (extraCategory != null) {
			activity.setCategory(extraCategory);
		}
	}

	public void saveCategory(Category category) {
		if (!categoryDao.isValid(category.getName()))
			return;

		String action = getActivity().getAction();
		if (Main.ACTION_ADD.equals(action)) {
			categoryDao.add(category.getName());
		} else if (Main.ACTION_EDIT.equals(action)) {
			extraCategory.setName(category.getName());
			categoryDao.update(extraCategory);
		}

		getActivity().goToMain();
	}

	public void cancel() {

	}

}
